/*
 * Copyright 2006 - 2007 The MITRE Corporation. All rights reserved.
 * Written by Ayal Spitz
 *
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 2.1
 * (the "License"); you may not use this file except in compliance
 * with the License and the following two conditions:
 * 
 * The US Government will not be charged any license fee and/or royalties 
 * related to this software.
 * 
 * Neither name of The MITRE Corporation; nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/copyleft/lesser.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.mitre.utils;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;

/**
 * The HashSetTable is an extension of Hashtable but instead of the hashtable
 * value there is a HashSet.
 * 
 * This implementation is not complete. It's only implemeted to the point that
 * satisfies the needs of this project.
 * 
 * @author Ayal Spitz
 */
public class HashSetTable<K, V> extends Hashtable<K, Set<V>>{
// ### Class Fields ###########################################################
	private static final long serialVersionUID = 390117405466423345L;

// ### Class Constructor ######################################################
	/**
	 * This constructor simply calls the Hashtable constructor
	 * 
	 * @see java.util.Hashtable#Constructor()
	 */
	public HashSetTable(){ super(); }

// ### Class Methods ##########################################################
	/**
	 * This method overrides the Hashtable put method. The method looks up the
	 * provided key and attempts to a value HashSet.  If the value HashSet does
	 * not exist then a new value HashSet is created and added to the hashtable.
	 * Then the value is added to the value HashSet.
	 * 
	 * @param key		The key
	 * @param value		The value
	 * 
	 * @see java.util.Hashtable.put
	 */
	public synchronized void put(K key, V value){
		Set<V>		valueSet = null;
		
		valueSet = super.get(key);
		if (valueSet == null){
			valueSet = new HashSet<V>();
			super.put(key, valueSet);
		}
		
		valueSet.add(value);
	}

	/**
	 * This method overrides the Hashtable putAll method. The method looks up
	 * the provided key and attempts to a value HashSet.  If the value HashSet
	 * does not exist then a new value HashSet is created and added to the 
	 * hashtable. Then the contents of the valueSet are added to the value HashSet.
	 * 
	 * @param key		The key
	 * @param valueSet	The valueSet to add the contents of
	 * 
	 * @see java.util.Hashtable.put
	 */
	public synchronized void putAll(K key, Set<V> valueSet){
		Set<V>		set = null;
		
		set = super.get(key);
		if (set == null){
			set = new HashSet<V>();
			super.put(key, set);
		}
		
		set.addAll(valueSet);
	}
	
	
}
